﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KMFrame.Core;
using NBearLite;
using LGCourt.Logic.NBL;
using LGCourt.Logic.Common;
using LGCourt.Logic.NBL.DBEntities;
using KMFrame.Util;

namespace LGCourt.Logic.BLL
{
    public class OA : LGCourtLogic
    {
        public vOAUser Login(string username, string password)
        {           
            return NBLData.Select(LGCourtColumns.vOAUser, GetOAUserCol())
                .Where(LGCourtColumns.vOAUser.username == username && LGCourtColumns.vOAUser.userpwd == Utils.DesEncrypt(password, "futureinfo"))
                .ToList<vOAUser>().FirstOrDefault();
        }

        private ExpressionClip[] GetOAUserCol()
        {
             return new ExpressionClip[] { 
                LGCourtColumns.vOAUser.userid, 
                LGCourtColumns.vOAUser.username,
                LGCourtColumns.vOAUser.realname,
                LGCourtColumns.vOAUser.userpwd,
                LGCourtColumns.vOAUser.deptname,
                LGCourtColumns.vOAUser.positionname,
                LGCourtColumns.vOAUser.groupids,
            };             
        }

        public vOAUser GetUserInfo(int userid)
        {
            List<vOAUser> oaUserList = NBLData.Select(LGCourtColumns.vOAUser, GetOAUserCol())
                .Where(LGCourtColumns.vOAUser.userid == userid)
                .ToList<vOAUser>();
            return oaUserList.Count <= 0 ? null : oaUserList[0];
        }      

        public vOAORGInfo GetOrgInfo()
        {
            return NBLData.Select(LGCourtColumns.vOAORGInfo, null)
                .ToSingleObject<vOAORGInfo>();
        }

        private ConditionCollection GetTongzhiGonggao(OANoticeCondition condition)
        {
            ConditionCollection cdtColl = new ConditionCollection();
            ExpressionClip[] selectColumns = null;
            cdtColl.SelectColumns = selectColumns;

            WhereClip where = LGCourtColumns.vOATongzhiGonggao.emp_id == condition.UserID;
            if (!string.IsNullOrEmpty(condition.nClass))
                where = where && LGCourtColumns.vOATongzhiGonggao.noticeclass == condition.nClass;
            if (!string.IsNullOrEmpty(condition.DeptName))
                where = where && LGCourtColumns.vOATongzhiGonggao.dept_name == condition.DeptName;
            if (condition.BeginDate.Year > 2000)
            {
                where = where && LGCourtColumns.vOATongzhiGonggao.pub_date >= condition.BeginDate;
            }
            if (condition.EndDate.Year > 2000)
            {
                where = where && LGCourtColumns.vOATongzhiGonggao.pub_date <= condition.EndDate;
            }
            if (!string.IsNullOrEmpty(condition.Keyword))
                where = where && (LGCourtColumns.vOATongzhiGonggao.title.Like("%" + condition.Keyword + "%") || LGCourtColumns.vOATongzhiGonggao.dept_name.Like("%" + condition.Keyword + "%"));
            cdtColl.Where = where;

            cdtColl.Select = NBLData.Select(LGCourtColumns.vOATongzhiGonggao, selectColumns)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), LGCourtColumns.vOATongzhiGonggao.id)
                .OrderBy(LGCourtColumns.vOATongzhiGonggao.pub_date.Desc);

            return cdtColl;
        }

        /// <summary>
        /// 通知公告
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public PageResult GetTongzhiGonggaoPage(OANoticeCondition condition)
        {
            PageResult result = new PageResult();

            result.ResultList = GetTongzhiGonggao(condition).Select.ToList<vOATongzhiGonggao>();

            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(LGCourtColumns.vOATongzhiGonggao, LGCourtColumns.vOATongzhiGonggao.id, GetTongzhiGonggao(condition).Where);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }


        private ConditionCollection GetPaiTouBing(OANoticeCondition condition)
        {
            ConditionCollection cdtColl = new ConditionCollection();
            ExpressionClip[] selectColumns = null;
            cdtColl.SelectColumns = selectColumns;

            WhereClip where = LGCourtColumns.vOAPaiTouBingZhuanLan.emp_id == condition.UserID;
            if (!string.IsNullOrEmpty(condition.nClass))
                where = where && LGCourtColumns.vOAPaiTouBingZhuanLan.noticeclass == condition.nClass;
            if (!string.IsNullOrEmpty(condition.DeptName))
                where = where && LGCourtColumns.vOAPaiTouBingZhuanLan.dept_name == condition.DeptName;
            if(condition.IsRead==0)
                where = where && LGCourtColumns.vOAPaiTouBingZhuanLan.readed == condition.IsRead;
            if (condition.BeginDate.Year > 2000)
            {
                where = where && LGCourtColumns.vOAPaiTouBingZhuanLan.pub_date >= condition.BeginDate;
            }
            if (condition.EndDate.Year > 2000)
            {
                where = where && LGCourtColumns.vOAPaiTouBingZhuanLan.pub_date <= condition.EndDate;
            }
            if (!string.IsNullOrEmpty(condition.Keyword))
                where = where && (LGCourtColumns.vOAPaiTouBingZhuanLan.title.Like("%" + condition.Keyword + "%") || LGCourtColumns.vOAPaiTouBingZhuanLan.dept_name.Like("%" + condition.Keyword + "%"));
            cdtColl.Where = where;

            cdtColl.Select = NBLData.Select(LGCourtColumns.vOAPaiTouBingZhuanLan, selectColumns)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), LGCourtColumns.vOAPaiTouBingZhuanLan.id)
                .OrderBy(LGCourtColumns.vOAPaiTouBingZhuanLan.pub_date.Desc);

            return cdtColl;
        }

        /// <summary>
        /// 排头兵
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public PageResult GetPaiTouBingPage(OANoticeCondition condition)
        {
            PageResult result = new PageResult();

            result.ResultList = GetPaiTouBing(condition).Select.ToList<vOAPaiTouBingZhuanLan>();

            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(LGCourtColumns.vOAPaiTouBingZhuanLan, LGCourtColumns.vOAPaiTouBingZhuanLan.id, GetPaiTouBing(condition).Where);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }

        private ConditionCollection GetXinXiBaoSong(OANoticeCondition condition)
        {
            ConditionCollection cdtColl = new ConditionCollection();
            ExpressionClip[] selectColumns = null;
            cdtColl.SelectColumns = selectColumns;

            WhereClip where =LGCourtColumns.vOAXinXiBaoSong.isactive == 1 && LGCourtColumns.vOAXinXiBaoSong.readed==0 && LGCourtColumns.vOAXinXiBaoSong.emp_id == condition.UserID;

            if (!string.IsNullOrEmpty(condition.Keyword))
                where = where && LGCourtColumns.vOAXinXiBaoSong.title.Like("%" + condition.Keyword + "%");
            if (!string.IsNullOrEmpty(condition.DeptName))
                where = where && LGCourtColumns.vOAXinXiBaoSong.dept_name == condition.DeptName;
            cdtColl.Where = where;

            cdtColl.Select = NBLData.Select(LGCourtColumns.vOAXinXiBaoSong, selectColumns)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), LGCourtColumns.vOAXinXiBaoSong.content)
                .OrderBy(LGCourtColumns.vOAXinXiBaoSong.fi_update_time.Desc);

            return cdtColl;
        }

        /// <summary>
        /// 信息报送
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public PageResult GetXinXiBaoSongPage(OANoticeCondition condition)
        {
            PageResult result = new PageResult();

            result.ResultList = GetXinXiBaoSong(condition).Select.ToList<vOAXinXiBaoSong>();

            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(LGCourtColumns.vOAXinXiBaoSong, LGCourtColumns.vOAXinXiBaoSong.content, GetXinXiBaoSong(condition).Where);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }

        private ConditionCollection GetFayuanYaowen(PageParam condition)
        {
            ConditionCollection cdtColl = new ConditionCollection();
            ExpressionClip[] selectColumns = null;
            cdtColl.SelectColumns = selectColumns;

            WhereClip where = null;
            cdtColl.Where = where;

            cdtColl.Select = NBLData.Select(LGCourtColumns.vOAFayuanYaowen, selectColumns)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), LGCourtColumns.vOAFayuanYaowen.id)
                .OrderBy(LGCourtColumns.vOAFayuanYaowen.id.Desc);

            return cdtColl;
        }

        /// <summary>
        /// 法院要闻
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public PageResult GetFayuanYaowenPage(PageParam condition)
        {
            PageResult result = new PageResult();

            result.ResultList = GetFayuanYaowen(condition).Select.ToList<vOAFayuanYaowen>();

            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(LGCourtColumns.vOAFayuanYaowen, LGCourtColumns.vOAFayuanYaowen.id, GetFayuanYaowen(condition).Where);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }

        private ConditionCollection GetXinxiZhuanlan(PageParam condition)
        {
            ConditionCollection cdtColl = new ConditionCollection();
            ExpressionClip[] selectColumns = null;
            cdtColl.SelectColumns = selectColumns;

            WhereClip where = null;

            cdtColl.Where = where;

            cdtColl.Select = NBLData.Select(LGCourtColumns.vOAXinxiZhuanlan, selectColumns)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), LGCourtColumns.vOAXinxiZhuanlan.id)
                .OrderBy(LGCourtColumns.vOAXinxiZhuanlan.id.Desc);

            return cdtColl;
        }

        /// <summary>
        /// 信息专栏
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public PageResult GetXinxiZhuanlanPage(PageParam condition)
        {
            PageResult result = new PageResult();

            result.ResultList = GetXinxiZhuanlan(condition).Select.ToList<vOAXinxiZhuanlan>();

            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(LGCourtColumns.vOAXinxiZhuanlan, LGCourtColumns.vOAXinxiZhuanlan.id, GetXinxiZhuanlan(condition).Where);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }

        private ConditionCollection GetWenhuaChanglang(PageParam condition)
        {
            ConditionCollection cdtColl = new ConditionCollection();
            ExpressionClip[] selectColumns = null;
            cdtColl.SelectColumns = selectColumns;

            WhereClip where = null;
            cdtColl.Where = where;

            cdtColl.Select = NBLData.Select(LGCourtColumns.vOAWenhuaChanglang, selectColumns)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), LGCourtColumns.vOAWenhuaChanglang.id)
                .OrderBy(LGCourtColumns.vOAWenhuaChanglang.id.Desc);

            return cdtColl;
        }

        /// <summary>
        /// 文化长廊
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public PageResult GetWenhuaChanglangPage(PageParam condition)
        {
            PageResult result = new PageResult();

            result.ResultList = GetWenhuaChanglang(condition).Select.ToList<vOAWenhuaChanglang>();

            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(LGCourtColumns.vOAWenhuaChanglang, LGCourtColumns.vOAWenhuaChanglang.id, GetWenhuaChanglang(condition).Where);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }

        private ConditionCollection GetDaibanShixiang(OAFlowCondition condition)
        {
            ConditionCollection cdtColl = new ConditionCollection();
            ExpressionClip[] selectColumns = null;
            cdtColl.SelectColumns = selectColumns;
            //((proposer_id="+u.ID.ToString()+" AND status=0) OR charindex(',"+u.ID.ToString()+",',','+current_actorids+',')>0) 
            WhereClip where = (LGCourtColumns.vOADaibanShixiang.cur_actor_id == condition.UserID) || (LGCourtColumns.vOADaibanShixiang.cur_actor_id != null && LGCourtColumns.vOADaibanShixiang.current_actorids.Like("%," + condition.UserID + ",%"));
            if (!string.IsNullOrEmpty(condition.Keyword))
                where = where && (LGCourtColumns.vOADaibanShixiang.title.Like("%" + condition.Keyword + "%") ||　LGCourtColumns.vOADaibanShixiang.source.Like("%" + condition.Keyword + "%"));
            

            cdtColl.Where = where;

            cdtColl.Select = NBLData.Select(LGCourtColumns.vOADaibanShixiang, selectColumns)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), LGCourtColumns.vOADaibanShixiang.id)
                .OrderBy(LGCourtColumns.vOADaibanShixiang.id.Desc);

            return cdtColl;
        }

        /// <summary>
        /// 待办事项
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public PageResult GetDaibanShixiangPage(OAFlowCondition condition)
        {
            PageResult result = new PageResult();

            result.ResultList = GetDaibanShixiang(condition).Select.ToList<vOADaibanShixiang>();

            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(LGCourtColumns.vOADaibanShixiang, LGCourtColumns.vOADaibanShixiang.id, GetDaibanShixiang(condition).Where);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }

        private ConditionCollection GetGongWenChuanyue(OAFlowCondition condition)
        {
            ConditionCollection cdtColl = new ConditionCollection();
            ExpressionClip[] selectColumns = null;
            cdtColl.SelectColumns = selectColumns;
            //((proposer_id="+u.ID.ToString()+" AND status=0) OR charindex(',"+u.ID.ToString()+",',','+current_actorids+',')>0) 
            WhereClip where = (LGCourtColumns.vOALiuChengChuanYue.readed == 0 && LGCourtColumns.vOALiuChengChuanYue.emp_id == condition.UserID);
            if (!string.IsNullOrEmpty(condition.Keyword))
                where = where && LGCourtColumns.vOALiuChengChuanYue.title.Like("%" + condition.Keyword + "%");            
            cdtColl.Where = where;

            cdtColl.Select = NBLData.Select(LGCourtColumns.vOALiuChengChuanYue, selectColumns)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), LGCourtColumns.vOALiuChengChuanYue.main_id)
                .OrderBy(LGCourtColumns.vOALiuChengChuanYue.send_time.Desc);

            return cdtColl;
        }

        /// <summary>
        /// 公文传阅
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public PageResult GetGongWenChuanyuePage(OAFlowCondition condition)
        {
            PageResult result = new PageResult();

            result.ResultList = GetGongWenChuanyue(condition).Select.ToList<vOALiuChengChuanYue>();

            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(LGCourtColumns.vOALiuChengChuanYue, LGCourtColumns.vOALiuChengChuanYue.main_id, GetGongWenChuanyue(condition).Where);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }

        private ConditionCollection GetShiwuShenqing(OACondition condition)
        {
            ConditionCollection cdtColl = new ConditionCollection();
            ExpressionClip[] selectColumns = null;
            cdtColl.SelectColumns = selectColumns;

            WhereClip where = LGCourtColumns.vOAShiwuShenqing.isactive == 1 && LGCourtColumns.vOAShiwuShenqing.flow_runtype != "系统启动";
            cdtColl.Where = where;

            cdtColl.Select = NBLData.Select(LGCourtColumns.vOAShiwuShenqing, selectColumns)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), LGCourtColumns.vOAShiwuShenqing.flowid)
                .OrderBy(LGCourtColumns.vOAShiwuShenqing.flowid.Desc);

            return cdtColl;
        }

        private bool IsExistsNewFlow(List<string> empids, string startuserids)
        {
            bool result = false;

            if (string.IsNullOrEmpty(startuserids))
                return result;
            startuserids = startuserids + ";";
            foreach (string id in empids)
            {
                if (startuserids.Contains(id + ";"))
                {
                    result = true;
                    break;
                }
            }
            return result;
        }

        /// <summary>
        /// 事务申请(首页)
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public List<vOAShiwuShenqing> GetShiwuShenqingPage(OACondition condition)
        {
            List<vOAShiwuShenqing> result = GetShiwuShenqingList(condition);
            result = PagingHandler.GetPageData<vOAShiwuShenqing>(result, condition.PageIndex, condition.PageSize);
            return result;
        }

        /// <summary>
        /// 事务申请(所有树）
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public List<vOAShiwuShenqing> GetShiwuShenqingList(OACondition condition)
        {
            List<vOAShiwuShenqing> result = new List<vOAShiwuShenqing>();           
            OAUserInfo userInfo = NBLData.Select(LGCourtColumns.vOAUser, GetOAUserCol())
               .Where(LGCourtColumns.vOAUser.userid == condition.UserID)
               .ToSingleObject<OAUserInfo>();
            WhereClip where = LGCourtColumns.vOAShiwuShenqing.isactive == 1 && LGCourtColumns.vOAShiwuShenqing.flow_runtype != "系统启动";
            List<vOAShiwuShenqing> list = NBLData.Select(LGCourtColumns.vOAShiwuShenqing)
                .Where(where)
                .ToList<vOAShiwuShenqing>();

            List<string> empids = new List<string>();
            if (userInfo != null && !string.IsNullOrEmpty(userInfo.groupids))
                empids = userInfo.groupids.Split(';').ToList();

            foreach (vOAShiwuShenqing info in list)
            {
                if (IsExistsNewFlow(empids, info.startuser_ids))
                {
                    result.Add(info);
                }
            }

            return result.OrderByDescending(a => a.class0).ToList();
        }

        /// <summary>
        /// 工作留言
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        private ConditionCollection GetGongZuoLiuYan(OAMessageCondition condition)
        {
            ConditionCollection cdtColl = new ConditionCollection();
            ExpressionClip[] selectColumns = null;
            cdtColl.SelectColumns = selectColumns;
            //((proposer_id="+u.ID.ToString()+" AND status=0) OR charindex(',"+u.ID.ToString()+",',','+current_actorids+',')>0) 
            WhereClip where = LGCourtColumns.vOAGongZuoLiuYan.emp_id == condition.UserID;
            if (condition.IsRead != -1)
            {
                where = where && LGCourtColumns.vOAGongZuoLiuYan.has_read == condition.IsRead;
            }
            if (!string.IsNullOrEmpty(condition.Keyword))
                where = where && LGCourtColumns.vOAGongZuoLiuYan.title.Like("%" + condition.Keyword + "%");
            cdtColl.Where = where;

            cdtColl.Select = NBLData.Select(LGCourtColumns.vOAGongZuoLiuYan, selectColumns)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), LGCourtColumns.vOAGongZuoLiuYan.id)
                .OrderBy(LGCourtColumns.vOAGongZuoLiuYan.id.Desc);

            return cdtColl;
        }

        /// <summary>
        /// 工作留言
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public PageResult GetGongZuoLiuYanPage(OAMessageCondition condition)
        {
            PageResult result = new PageResult();

            result.ResultList = GetGongZuoLiuYan(condition).Select.ToList<vOAGongZuoLiuYan>();

            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(LGCourtColumns.vOAGongZuoLiuYan, LGCourtColumns.vOAGongZuoLiuYan.id, GetGongZuoLiuYan(condition).Where);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }


        private ConditionCollection GetTongjiBaobiao(PageParam condition)
        {
            ConditionCollection cdtColl = new ConditionCollection();
            ExpressionClip[] selectColumns = null;
            cdtColl.SelectColumns = selectColumns;

            WhereClip where = null;
            cdtColl.Where = where;

            cdtColl.Select = NBLData.Select(LGCourtColumns.vOATongjiBaobiao, selectColumns)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), LGCourtColumns.vOATongjiBaobiao.id)
                .OrderBy(LGCourtColumns.vOATongjiBaobiao.id.Desc);

            return cdtColl;
        }
        public List<vOATongjiBaobiao> GetTongjiBaobiaoTop(OACondition condition)
        {
            List<vOATongjiBaobiao> result = GetTongjiBaobiaoList(condition);
            result = PagingHandler.GetPageData<vOATongjiBaobiao>(result, condition.PageIndex, condition.PageSize);
            return result;
        }


        /// <summary>
        /// 统计报表
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public List<vOATongjiBaobiao> GetTongjiBaobiaoList(OACondition condition)
        {
            List<vOATongjiBaobiao> result = new List<vOATongjiBaobiao>();

            OAUserInfo userInfo = NBLData.Select(LGCourtColumns.vOAUser, GetOAUserCol())
               .Where(LGCourtColumns.vOAUser.userid == condition.UserID)
               .ToSingleObject<OAUserInfo>();
            WhereClip where = WhereClip.All;
            List<vOATongjiBaobiao> list = NBLData.Select(LGCourtColumns.vOATongjiBaobiao)
                .Where(where)
                .OrderBy(LGCourtColumns.vOATongjiBaobiao.uclass.Desc)
                .ToList<vOATongjiBaobiao>();

            List<string> empids = new List<string>();
            if (userInfo != null && !string.IsNullOrEmpty(userInfo.groupids))
                empids = userInfo.groupids.Split(';').ToList();

            foreach (vOATongjiBaobiao info in list)
            {
                if (!string.IsNullOrEmpty(info.class_ids))
                {
                    if (IsExistsNewFlow(empids, info.class_ids))
                    {
                        result.Add(info);
                    }
                }
                else
                {
                    result.Add(info);
                }
            }
            //  result = PagingHandler.GetPageData<vOATongjiBaobiao>(resultList, condition.PageIndex, condition.PageSize);

            return result.OrderByDescending(a => a.uclass).ToList();
        }

        private ConditionCollection GetKaitingPaiqi(OACondition condition)
        {
            ConditionCollection cdtColl = new ConditionCollection();
            ExpressionClip[] selectColumns = null;
            cdtColl.SelectColumns = selectColumns;

            WhereClip where = null;
            cdtColl.Where = where;

            cdtColl.Select = NBLData.Select(LGCourtColumns.pKTPQ, selectColumns)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), LGCourtColumns.pKTPQ.KTPQID)
                .OrderBy(LGCourtColumns.pKTPQ.KTPQID.Desc);

            return cdtColl;
        }

        /// <summary>
        /// 开庭排期
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public PageResult GetKaitingPaiqiPage(OACondition condition)
        {
            PageResult result = new PageResult();

            result.ResultList = GetKaitingPaiqi(condition).Select.ToList<pKTPQ>();

            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(LGCourtColumns.pKTPQ, LGCourtColumns.pKTPQ.KTPQID, GetKaitingPaiqi(condition).Where);
            }
            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }

        private ConditionCollection GetTongxunluCondition(OACondition condition)
        {
            ConditionCollection cdtColl = new ConditionCollection();
            ExpressionClip[] selectColumns = null;
            cdtColl.SelectColumns = selectColumns;

            WhereClip where = WhereClip.All;
            if (!string.IsNullOrEmpty(condition.Keyword))
                where = where && LGCourtColumns.vOATongXunLu.name.Like("%" + condition.Keyword + "%");
            if (!string.IsNullOrEmpty(condition.DeptName))
                where = where && LGCourtColumns.vOATongXunLu.dept_name==condition.DeptName;
            cdtColl.Where = where;
            cdtColl.Select = NBLData.Select(LGCourtColumns.vOATongXunLu, selectColumns)
                .Where(where)
                .SetSelectRange(condition.PageSize, condition.PageSize * (condition.PageIndex - 1), LGCourtColumns.vOATongXunLu.id)
                .OrderBy(LGCourtColumns.vOATongXunLu.fi_order.Asc);

            return cdtColl;
        }

        /// <summary>
        /// 通讯录
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public PageResult GetTongxunluPage(OACondition condition)
        {
            PageResult result = new PageResult();

            result.ResultList = GetTongxunluCondition(condition).Select.ToList<vOATongXunLu>();

            int total = -1;
            if (condition.TotalCount == -1)
            {
                total = GetTotalRecord(LGCourtColumns.vOATongXunLu, LGCourtColumns.vOATongXunLu.id, GetTongxunluCondition(condition).Where);
            }

            result.PageIndex = condition.PageIndex;
            result.PageSize = condition.PageSize;
            result.TotalCount = total;
            return result;
        }

    }

    public class OAUserInfo
    {
        public int userid { get; set; }
        public string username { get; set; }
        public string realname { get; set; }
        public string password { get; set; }
        public string dept_name {get;set;}
        public string position_name {get;set;}
        public string groupids { get; set; }
        //public Guid userguid { get; set; }        
    }

    public class OACondition : PageParam
    {
        public int UserID { get; set; }
        public string Keyword { get; set; }
        public string DeptName { get; set; }
    }

    public class OANoticeCondition : PageParam
    {
        public int UserID { get; set; }
        public string Keyword { get; set; }
        public string nClass { get; set; }
        public string DeptName { get; set; }
        public int IsRead { get; set; }
        public DateTime BeginDate { get; set; }
        public DateTime EndDate { get; set; }        
    }

    public class OAFlowCondition : PageParam
    {
        public int UserID { get; set; }
        public string Keyword { get; set; }
        public DateTime? BeginTime { get; set; }
        public string DeptName { get; set; }
    }

    public class OAMessageCondition : PageParam
    {
        public int UserID { get; set; }
        public string Keyword { get; set; }
        public DateTime? BeginTime { get; set; }
        public int IsRead { get; set; }
    }
}
